package com.yy.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yy.common.constant.PmsConstants;
import com.yy.common.exception.ServiceException;
import com.yy.common.utils.DateUtils;
import com.yy.dto.OaRepairDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yy.mapper.OaRepairMapper;
import com.yy.domain.OaRepair;
import com.yy.service.IOaRepairService;

/**
 * 维修工单Service业务层处理
 * 
 * @author yy
 * @date 2023-11-03
 */
@Service
public class OaRepairServiceImpl implements IOaRepairService 
{
    @Autowired
    private OaRepairMapper oaRepairMapper;

    /**
     * 查询维修工单
     * 
     * @param id 维修工单主键
     * @return 维修工单
     */
    @Override
    public OaRepair selectOaRepairById(Long id)
    {
        return oaRepairMapper.selectOaRepairById(id);
    }

    /**
     * 查询维修工单列表
     * 
     * @param oaRepair 维修工单
     * @return 维修工单
     */
    @Override
    public List<OaRepair> selectOaRepairList(OaRepair oaRepair)
    {
        return oaRepairMapper.selectOaRepairList(oaRepair);
    }

    /**
     * 新增维修工单
     * 
     * @param oaRepair 维修工单
     * @return 结果
     */
    @Override
    public int insertOaRepair(OaRepair oaRepair)
    {
        oaRepair.setCreateTime(DateUtils.getNowDate());
        return oaRepairMapper.insertOaRepair(oaRepair);
    }

    /**
     * 受理工单
     *
     * @param repairDTO
     * @return
     */
    @Override
    public int applyRepair(OaRepairDTO repairDTO) {
        //根据ID查询工单信息
        OaRepair oaRepair = this.oaRepairMapper.selectOaRepairById(repairDTO.getId());
        if(ObjectUtil.isNull(oaRepair)){
            throw new ServiceException("工单不存在");
        }
        if(!oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_APPLY)){
            throw new ServiceException("工单不是待受理状态，不能受理");
        }
        //设置受理时间
        oaRepair.setAppTime(new Date());
        //设置受理说明
        oaRepair.setAppContent(repairDTO.getContent());
        //设置维修人的ID
        oaRepair.setRepairUser(repairDTO.getRepairUser().toString());
        //设置维修人员的名称
        oaRepair.setRepairUserName(repairDTO.getRepairUserName());
        //设置当前工单的状态为处理中
        oaRepair.setRepairState(PmsConstants.OA_REPAIR_STATE_UNCONFIRMED);
        return this.oaRepairMapper.updateOaRepair(oaRepair);
    }

    /**
     * 现场确认
     *
     * @param repairDTO
     * @return
     */
    @Override
    public int confirmedRepair(OaRepairDTO repairDTO) {
        //根据ID查询工单信息
        OaRepair oaRepair = this.oaRepairMapper.selectOaRepairById(repairDTO.getId());
        if(ObjectUtil.isNull(oaRepair)){
            throw new ServiceException("工单不存在");
        }
        if(!oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_UNCONFIRMED)){
            throw new ServiceException("工单不是待现场确认状态，不能待现场确认");
        }
        //设置问题确认备注
        oaRepair.setConfirmContent(repairDTO.getContent());
        //设置维修材料费用
        oaRepair.setRepairMaterialsFee(repairDTO.getRepairMaterialsFee());
        //设置维修服务费用
        oaRepair.setRepairServiceFee(repairDTO.getRepairServiceFee());
        //设置当前工单的状态为维修中
        oaRepair.setRepairState(PmsConstants.OA_REPAIR_STATE_REPAIRING);
        return this.oaRepairMapper.updateOaRepair(oaRepair);
    }

    /**
     * 维修确认
     *
     * @param repairDTO
     * @return
     */
    @Override
    public int doRepairConfirm(OaRepairDTO repairDTO) {
        //根据ID查询工单信息
        OaRepair oaRepair = this.oaRepairMapper.selectOaRepairById(repairDTO.getId());
        if(ObjectUtil.isNull(oaRepair)){
            throw new ServiceException("工单不存在");
        }
        if(!oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_REPAIRING)){
            throw new ServiceException("工单不是维修中的状态，不能待维修确认");
        }
        //设置修理备注
        oaRepair.setRepairContent(repairDTO.getContent());
        //设置当前工单的状态为待评价
        oaRepair.setRepairState(PmsConstants.OA_REPAIR_STATE_SCORE);
        return this.oaRepairMapper.updateOaRepair(oaRepair);
    }

    /**
     * 维修评价
     *
     * @param repairDTO
     * @return
     */
    @Override
    public int doRepairScore(OaRepairDTO repairDTO) {
        //根据ID查询工单信息
        OaRepair oaRepair = this.oaRepairMapper.selectOaRepairById(repairDTO.getId());
        if(ObjectUtil.isNull(oaRepair)){
            throw new ServiceException("工单不存在");
        }
        if(!oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_SCORE)){
            throw new ServiceException("工单不是待评价的状态，不能待评价");
        }
        //设置评价分数
        oaRepair.setOwnerScore(repairDTO.getOwnerScore());
        //设置评价备注
        oaRepair.setOwnerScoreRemark(repairDTO.getOwnerScoreRemark());
        //更改状态
        oaRepair.setRepairState(PmsConstants.OA_REPAIR_STATE_COMPLETE);
        //计算总耗时
        Date applyTime = oaRepair.getApplyTime();
        //计算当前时间和受理时间之间的小时差
        oaRepair.setTimeConsum(DateUtil.between(applyTime,DateUtil.date(), DateUnit.HOUR));
        oaRepair.setEndTime(new Date());
        return this.oaRepairMapper.updateOaRepair(oaRepair);
    }

    /**
     * 物业回访
     *
     * @param repairDTO
     * @return
     */
    @Override
    public int doRepairVisit(OaRepairDTO repairDTO) {
        //根据ID查询工单信息
        OaRepair oaRepair = this.oaRepairMapper.selectOaRepairById(repairDTO.getId());
        if(ObjectUtil.isNull(oaRepair)){
            throw new ServiceException("工单不存在");
        }
        if(oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_COMPLETE)||oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_CANCEL)){
            //设置物业回访评价分数
            oaRepair.setReturnResult(repairDTO.getReturnResult());
            //设置物业回访评价备注
            oaRepair.setReturnRemark(repairDTO.getReturnRemark());
            //更改回访状态 已回访
            oaRepair.setReturnState(PmsConstants.OA_RETURN_STATE_HAS_VISIT);
            return this.oaRepairMapper.updateOaRepair(oaRepair);
        }else{
            throw new ServiceException("工单未完成，不能添加回访");
        }
    }

    /**
     * 撤销
     *
     * @param id
     * @return
     */
    @Override
    public int doCancel(Long id) {
        //根据ID查询工单
        OaRepair oaRepair = this.oaRepairMapper.selectOaRepairById(id);
        if(oaRepair==null){
            throw new ServiceException("工单不存在");
        }
        if(oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_SCORE)){
            throw new ServiceException("当前工单是待评价状态,不能撤销");
        }
        if(oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_COMPLETE)){
            throw new ServiceException("当前工单是完成状态,不能撤销");
        }
        if(oaRepair.getRepairState().equals(PmsConstants.OA_REPAIR_STATE_CANCEL)){
            throw new ServiceException("当前工单是撤销状态,不能撤销");
        }
        oaRepair.setRepairState(PmsConstants.OA_REPAIR_STATE_CANCEL);
        return this.oaRepairMapper.updateOaRepair(oaRepair);
    }


}
